<template>
  <div class="app-container">
    <el-row>
      <el-col   class="mb8" :span="4.5" style="display: flex; justify-content: flex-end;">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
              >活动申请</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
              >批量删除</el-button>
          </el-col>
        </el-col>
      </el-row>
    <el-table v-loading="loading" :data="activitiesList" @selection-change="handleSelectionChange" v-if="showTable1">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公寓名称" align="center" prop="apartmentName" />
      <el-table-column label="活动日期" align="center" prop="activityDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动时间" align="center" prop="activityTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动名称" align="center" prop="activityName" />
      <el-table-column label="活动描述" align="center" prop="activityDescription" />
      <el-table-column label="活动负责人" align="center" prop="leaderName" />
      <el-table-column label="参与人数" align="center" prop="participantCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:activities:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:activities:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table v-loading="loading" :data="activitiesList" @selection-change="handleSelectionChange" v-else-if="showTable2">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公寓名称" align="center" prop="apartmentName" />
      <el-table-column label="活动日期" align="center" prop="activityDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动时间" align="center" prop="activityTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动名称" align="center" prop="activityName" />
      <el-table-column label="活动描述" align="center" prop="activityDescription" />
      <el-table-column label="活动负责人" align="center" prop="leaderName" />
      <el-table-column label="参与人数" align="center" prop="participantCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动信息的对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属公寓" prop="apartmentId" v-if="IsAdd">
          <el-select clearable v-model="form.apartmentId" placeholder="请选择公寓名称">
            <el-option v-for="option in apartmentList" :key="option.apartmentId" :label="option.apartmentName" :value="option.apartmentId" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属公寓" prop="apartmentName" v-else>
          <el-select clearable v-model="form.apartmentId" placeholder="请选择公寓名称">
            <el-option v-for="option in apartmentList" :key="option.apartmentId" :label="option.apartmentName" :value="option.apartmentId" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动日期" prop="activityDate">
          <el-date-picker clearable
            v-model="form.activityDate"
            type="date"
            value-format="timestamp"
            placeholder="请选择活动日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动时间" prop="activityTime">
          <el-date-picker clearable
            v-model="form.activityTime"
            type="date"
            value-format="timestamp"
            placeholder="请选择活动时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动名称" prop="activityName">
          <el-input v-model="form.activityName" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="活动描述" prop="activityDescription">
          <el-input v-model="form.activityDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动负责人" prop="leaderName">
          <el-select clearable v-model="form.leaderName" placeholder="请选择活动负责人">
            <el-option v-for="item in workUserList" :key="item.userId" :label="item.nickName" :value="item.nickName" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与人数" prop="participantCount">
          <el-input v-model="form.participantCount" placeholder="请输入参与人数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivities, getActivities, delActivities, addActivities, updateActivities } from "@/api/business/activities";
import { listElderlyapartment, getElderlyapartment, delElderlyapartment, addElderlyapartment, updateElderlyapartment } from "@/api/business/elderlyapartment";
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus, deptTreeSelect,commonUserList } from "@/api/system/user";


export default {
  props: {
    showTable1: {
      type: Boolean,
      default: false
    },
    showTable2: {
      type: Boolean,
      default: false
    },
    list:[]
  },
  name: "Activities",
  data() {
    return {
      isApprove: true,
      IsAdd: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      activitiesList: [],
      // 公寓信息
      apartmentList: [],
      //普通用户list
      workUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        apartmentId: null,
        activityDate: null,
        activityTime: null,
        activityName: null,
        activityDescription: null,
        applyStatus:1,
        leaderName: null,
        participantCount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        apartmentId: [
          { required: true, message: "公寓ID不能为空", trigger: "blur" }
        ],
        activityDate: [
          { required: true, message: "活动日期不能为空", trigger: "blur" }
        ],
        activityTime: [
          { required: true, message: "活动时间不能为空", trigger: "blur" }
        ],
        activityName: [
          { required: true, message: "活动名称不能为空", trigger: "blur" }
        ],
        leaderName: [
          { required: true, message: "活动负责人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {},
  methods: {
    getCommonUserList(){
      commonUserList().then(response =>{
        this.workUserList = response.rows
        console.log(this.workUserList);
      })
    },
    //获取公寓名称列表
    getApartmentList(){
      listElderlyapartment().then(response =>{
        this.apartmentList = response.result;
      });
      listUser().then(response =>{
        var list = [];
        //todo
        response.rows.filter((p)=>{
          if(p.deptId != 103 && p.deptId!=105){
            list.push(p);
          }
          console.log(list)
          return list
        })
        this.workUserList =  list
      })
    },
    /** 查询活动列表 */
    getList() {
      this.loading = true;
      listActivities(this.queryParams).then(response => {
        this.activitiesList = response.result;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        apartmentId: null,
        activityDate: null,
        activityTime: null,
        activityName: null,
        activityDescription: null,
        leaderName: null,
        participantCount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivities(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.from.createUserId = this.workUserList.createUserId
            updateActivities(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivities(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delActivities(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/activities/export', {
        ...this.queryParams
      }, `activities_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
  .mb8{
    margin-right: 10px;
  }
</style>